package CollegeInformation;

import javax.swing.*;
import java.awt.event.*;

public class updatateacher extends JDialog {
    private JPanel contentPane;
    private JButton buttonOK;
    private JButton buttonCancel;
    private JTextField textField1;
    private JTextField textField2;
    private JTextField textField3;
    private JTextField textField4;
    private JTextField textField6;
    private JTextField textField5;
    private JTextField textField7;
    private JTextField textField8;
    private JTextField textField9;

    public updatateacher(){};
    public updatateacher(Employee employee) {
        setContentPane(contentPane);
        setModal(true);
        getRootPane().setDefaultButton(buttonOK);
//        如果员工和教师类对应的话，接受数据库的值
        if(employee instanceof Teacher){
            textField1.setText(employee.getName());
            textField2.setText(employee.getSex());
            textField3.setText(employee.getAge()+"");
            textField4.setText(((Teacher)employee).getDepartment());
            textField5.setText(((Teacher)employee).getMajor());
            textField6.setText(((Teacher)employee).getTitle());
        }else if(employee instanceof Officer){
            textField1.setText(employee.getName());
            textField2.setText(employee.getSex());
            textField3.setText(employee.getAge()+"");
            textField9.setText(((Officer)employee).getPolitical());
            textField6.setText(((Officer)employee).getOtitle());

        }else if(employee instanceof LabMember){
            textField1.setText(employee.getName());
            textField2.setText(employee.getSex());
            textField3.setText(employee.getAge()+"");
            textField7.setText(((LabMember)employee).getLab());
            textField8.setText(((LabMember)employee).getJob());
        }else if(employee instanceof LabMember){
            textField1.setText(employee.getName());
            textField2.setText(employee.getSex());
            textField3.setText(employee.getAge()+"");
            textField4.setText(((TOfficer)employee).getDepartment());
            textField5.setText(((TOfficer)employee).getMajor());
            textField6.setText(((TOfficer)employee).getTitle());
            textField9.setText(((TOfficer)employee).getTOpolitical());
        }

        buttonOK.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                String name,sex,department,major,title,lab,job,political;
                int age;
                String i=employee.getId();
                String t=employee.getType();
                if(t.equalsIgnoreCase("teacher")){
                    name=textField1.getText();
                    sex=textField2.getText();
                    age=Integer.parseInt(textField3.getText());
                    department=textField4.getText();
                    major=textField5.getText();
                    title=textField6.getText();
                    Teacher teacher=new Teacher(i,name,sex,age,t,department,major,title);
                    Service2.modify(teacher);
                }
                if(t.equalsIgnoreCase("labmember")){
                    name=textField1.getText();
                    sex=textField2.getText();
                    age=Integer.parseInt(textField3.getText());
                    lab=textField5.getText();
                    job=textField6.getText();
                    LabMember labMember=new LabMember(i,name,sex,age,t,lab,job);
                    Service2.modify(labMember);
                }
                if(t.equalsIgnoreCase("officer")){
                    name=textField1.getText();
                    sex=textField2.getText();
                    age=Integer.parseInt(textField3.getText());
                    political=textField9.getText();
                    title=textField6.getText();
                    Officer officer=new Officer(i,name,sex,age,t,political,title);
                    Service2.modify(officer);
                }
                if(t.equalsIgnoreCase("tofficer")){
                    name=textField1.getText();
                    sex=textField2.getText();
                    age=Integer.parseInt(textField3.getText());
                    department=textField4.getText();
                    major=textField5.getText();
                    title=textField6.getText();
                    political=textField9.getText();
                    TOfficer tOfficer=new TOfficer(i,name,sex,age,t,department,major,title
                            ,political);
                    Service2.modify(tOfficer);
                }
                dispose();
            }
        });
        buttonCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        });

        // 单击十字时调用 onCancel()
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });

        // 遇到 ESCAPE 时调用 onCancel()
        contentPane.registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    }


    private void onCancel() {
        // 必要时在此处添加您的代码
        dispose();
    }


}
